package com.founder.autoCode.template;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.founder.autoCode.ExcelUtil;

public class ServiceClassDataParser implements ClassDataParser {
	
	private static final String FUNCTION_NAMES = "functionNames";
	
	private static final String FUNCTION_NAME = "name";
	
	private static final String FUNCTION_COMMENT = "comment";
	
	private static final String RETURN_TYPE = "return";
	
	private static final int PACKAGE_INDEX = 1;
	
	private static final int CLASS_NAME_INDEX = 7;
	
	private static final int FUNCTION_COMMENT_INDEX = 8;
	
	private static final int FUNCTION_NAME_INDEX = 9;
	
	private static final int FUNCTION_RETURN_TYPE_INDEX = 10;
	
	private static final int FUNCTION_PARAM_INDEX = 12;
	
	public void parse(Sheet sheet, List datasource) {
		int rowNum = sheet.getLastRowNum();
		for (int i = 1; i <= rowNum; i++) {
			Row ros = sheet.getRow(i);
			//类名
			String className = ExcelUtil.getString(ros, CLASS_NAME_INDEX);
			//方法名
			String functionName = ExcelUtil.getString(ros, FUNCTION_NAME_INDEX);
			System.out.println("className=" + className + ", functionName=" + functionName);
			if (!"".equals(className)) {
				Map classMap = new HashMap();
				datasource.add(classMap);
				
				//包名
				classMap.put(PACKAGE_NAME, ExcelUtil.getString(ros, PACKAGE_INDEX));
				classMap.put(CLASS_NAME, className);
			} else if (!"".equals(functionName)) {
				int size = datasource.size();
				if (size == 0) {
					continue;
				}
				Map classMap = (Map)datasource.get(size - 1);
				List functionNames = (List)classMap.get(FUNCTION_NAMES);
				if (functionNames == null) {
					functionNames = new ArrayList();
					classMap.put(FUNCTION_NAMES, functionNames);
				}
				Map functionMap = new HashMap();
				functionNames.add(functionMap);
				
				functionMap.put(FUNCTION_NAME, functionName);
				
				//方法注释
				String comment = ExcelUtil.getString(ros, FUNCTION_COMMENT_INDEX);
				functionMap.put(FUNCTION_COMMENT, comment);
				
				//返回值类型
				String retType = ExcelUtil.getString(ros, FUNCTION_RETURN_TYPE_INDEX);
				if ("".equals(retType)) {
					functionMap.put(RETURN_TYPE, "void");
				} else {
					functionMap.put(RETURN_TYPE, retType);
				}
				
				//参数
				String paramName = ExcelUtil.getString(ros, FUNCTION_PARAM_INDEX + 1);
				if (!"".equals(paramName)) {
					List params = new ArrayList();
					functionMap.put("params", params);
					for (int j = 0; j < 5; j++) {
						int index = FUNCTION_PARAM_INDEX + j * 4;
						paramName = ExcelUtil.getString(ros, index + 1);
						if (!"".equals(paramName)) {
							Map paramMap = getParamMap(ros, index);
							params.add(paramMap);
						}
					}
				}
			}
		}
	}
	
	private Map getParamMap(Row ros, int index) {
		Map paramMap = new HashMap();
		paramMap.put("type", ExcelUtil.getString(ros, index));
		paramMap.put("name", ExcelUtil.getString(ros, index + 1));
		return paramMap;
	}

}
